library(readxl)
mydata <- read_xlsx("./AI.xlsx")
mydata <- as.data.frame(mydata)

head(mydata)
##   ID AIU1 AIU2 AIU3
## 1  1    1    1    3
## 2  2    3    2    3
## 3  3    2    2    2
## 4  4    2    4    4
## 5  5    3    3    4
## 6  6    2    1    3

Description:

library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
library(tidyverse)
## ── Attaching core tidyverse packages ─────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2
## ── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks rstatix::filter(), stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mydata_long <- mydata %>%
  pivot_longer(
    cols = c("AIU1", "AIU2", "AIU3"),
    names_to = "AIU",
    values_to = "Agreement") %>%
  convert_as_factor(AIU)
head(mydata_long)
## # A tibble: 6 × 3
##      ID AIU   Agreement
##   <dbl> <fct>     <dbl>
## 1     1 AIU1          1
## 2     1 AIU2          1
## 3     1 AIU3          3
## 4     2 AIU1          3
## 5     2 AIU2          2
## 6     2 AIU3          3
library(ggplot2)
ggplot(mydata_long, aes(x = Agreement)) +
  geom_bar(fill = "grey") +
  facet_wrap(~ AIU) +
  scale_x_continuous(
    breaks = 1:7,
    limits = c(0.5, 7.5)
  ) +
  labs(
    x = "Agreement (1 = Agree strongly, 7 = Disagree strongly)",
    y = "Number of students"
  ) +
  theme_bw()

library(ggplot2)
ggplot(mydata_long, aes(y = Agreement, x = AIU)) +
  geom_boxplot(fill = "grey") +
  geom_point() + 
  scale_y_continuous(breaks = seq(1, 7, 1), limits = c(1, 7)) +
  theme_bw()

library(afex)
## Loading required package: lme4
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## Registered S3 method overwritten by 'lme4':
##   method           from
##   na.action.merMod car
## ************
## Welcome to afex. For support visit: http://afex.singmann.science/
## - Functions for ANOVAs: aov_car(), aov_ez(), and aov_4()
## - Methods for calculating p-values with mixed(): 'S', 'KR', 'LRT', and 'PB'
## - 'afex_aov' and 'mixed' objects can be passed to emmeans() for follow-up tests
## - Get and set global package options with: afex_options()
## - Set sum-to-zero contrasts globally: set_sum_contrasts()
## - For example analyses see: browseVignettes("afex")
## ************
## 
## Attaching package: 'afex'
## The following object is masked from 'package:lme4':
## 
##     lmer
rm_model <- aov_ez(
  id = "ID",        # subject identifier (repeated‑measures factor)
  dv = "Agreement",   # dependent variable
  within = "AIU",  # within‑subjects factor (three statements)
  data = mydata_long
)
residuals_rm <- residuals(rm_model$lm)
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
qqPlot(
  residuals_rm,
  distribution = "norm",
  envelope = TRUE,
  col = "grey40",
  pch = 19,
  main = "Normal Q–Q Plot of Repeated-Measures ANOVA Residuals",
  id = FALSE
)

library(rstatix)
library(dplyr)
mydata_long %>%
  group_by(AIU) %>%
  shapiro_test(Agreement)
## # A tibble: 3 × 4
##   AIU   variable  statistic          p
##   <fct> <chr>         <dbl>      <dbl>
## 1 AIU1  Agreement     0.808 0.00000363
## 2 AIU2  Agreement     0.841 0.0000213 
## 3 AIU3  Agreement     0.912 0.00225
library(rstatix)

mydata_long %>%
  group_by(AIU) %>%
  get_summary_stats(Agreement, type = "five_number")
## # A tibble: 3 × 8
##   AIU   variable      n   min   max    q1 median    q3
##   <fct> <fct>     <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl>
## 1 AIU1  Agreement    45     1     3     1      2     3
## 2 AIU2  Agreement    45     1     4     1      2     3
## 3 AIU3  Agreement    45     1     6     2      3     4
library(rstatix)

#Friedman ANOVA.
FriedmanANOVA <- friedman_test(Agreement ~ AIU | ID,
                               data = mydata_long)

FriedmanANOVA #Summary of results.
## # A tibble: 1 × 6
##   .y.           n statistic    df             p method       
## * <chr>     <int>     <dbl> <dbl>         <dbl> <chr>        
## 1 Agreement    45      37.2     2 0.00000000848 Friedman test
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
## 
##     cohens_d, eta_squared, omega_squared
effectsize::kendalls_w(Agreement ~ AIU | ID,
                       data = mydata_long)
## Warning: 37 block(s) contain ties, some containing only 1 unique ranking.
## Kendall's W |       95% CI
## --------------------------
## 0.41        | [0.28, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
interpret_kendalls_w(0.41)
## [1] "moderate agreement"
## (Rules: landis1977)
library(rstatix)

#Wilcoxon signed rank tests - comparing all possible paires.
pairs_nonpar <- wilcox_test(Agreement ~ AIU, 
                             paired = TRUE, 
                             p.adjust.method = "bonferroni",
                             data = mydata_long)

pairs_nonpar
## # A tibble: 3 × 9
##   .y.      group1 group2    n1    n2 statistic       p   p.adj p.adj.signif
## * <chr>    <chr>  <chr>  <int> <int>     <dbl>   <dbl>   <dbl> <chr>       
## 1 Agreeme… AIU1   AIU2      45    45     217   1.40e-1 4.21e-1 ns          
## 2 Agreeme… AIU1   AIU3      45    45      54.5 2.46e-8 7.39e-8 ****        
## 3 Agreeme… AIU2   AIU3      45    45     134   2.19e-5 6.58e-5 ****